package com.gable.report.service.impl;


import com.gable.report.dao.IChartsAreaSearchDao;
import com.gable.report.model.ChartsPriceCategory;
import com.gable.report.model.ChartsSearchView;
import com.gable.report.model.User;
import com.gable.report.service.ChartsAreaSearchService;
import com.gable.report.thread.RedisListStoreThread;
import com.gable.report.util.RedisUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.gable.report.util.ChartsUtil.getPinYin;
import static com.gable.report.util.ChartsUtil.getSearchParamMap;

/**
 * Created by lvdouStart on 2017/7/18.
 * 政府报表筛选service实现类(包括市级和区级)
 */
@Service
public class ChartsAreaSearchServiceImpl implements ChartsAreaSearchService {

    private final Log logger = LogFactory.getLog(ChartsAreaSearchServiceImpl.class);

    @Autowired
    private IChartsAreaSearchDao chartsAreaSearchDao;

    private RedisTemplate redisTemplate;

    @Autowired(required = false)
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //设置redis数据库
        //JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory)redisTemplate.getConnectionFactory();
        //jedisConnectionFactory.setDatabase(2);
        this.redisTemplate = redisTemplate;
    }

    @Override
    public List<ChartsSearchView> queryAreaHospitals(User user) {
        Map<String,Object> paramMap = getSearchParamMap(user);
        List<ChartsSearchView> returnList;
        if(RedisUtil.isOn){
            String key = RedisUtil.getAreaSearchKey(user,"hospital");
            List list = RedisUtil.getListValue(redisTemplate,key);
            if(list == null){
                returnList = chartsAreaSearchDao.queryAreaHospitals(paramMap);
                // 把数据存入缓存
                new RedisListStoreThread(redisTemplate,key,returnList).run();
            }else{
                returnList = list;
            }
        }else{
            returnList = chartsAreaSearchDao.queryAreaHospitals(paramMap);
        }
        return getPinYin(returnList);
    }

    @Override
    public List<ChartsSearchView> queryHospitalLevels(User user) {
        Map<String,Object> paramMap = getSearchParamMap(user);
        List<ChartsSearchView> returnList;
        if(RedisUtil.isOn){
            String key = RedisUtil.getAreaSearchKey(user,"level");
            List list = RedisUtil.getListValue(redisTemplate,key);
            if(list == null){
                returnList = chartsAreaSearchDao.queryHospitalLevels(paramMap);
                // 把数据存入缓存
                new RedisListStoreThread(redisTemplate,key,returnList).run();
            }else{
                returnList = list;
            }
        }else{
            returnList = chartsAreaSearchDao.queryHospitalLevels(paramMap);
        }
        return getPinYin(returnList);
    }

    @Override
    public List<ChartsSearchView> queryDeviceRiskLevels() {
        ChartsSearchView csv0 = new ChartsSearchView("0","〇级","zeroth");
        ChartsSearchView csv1 = new ChartsSearchView("1","Ⅰ级","first");
        ChartsSearchView csv2 = new ChartsSearchView("2","Ⅱ级","secondary");
        ChartsSearchView csv3 = new ChartsSearchView("3","Ⅲ级","tertiary");
        return Arrays.asList(csv3,csv2,csv1,csv0);
    }

    @Override
    public Map<String,Object> queryDeviceTypes(User user) {
        Map<String,Object> paramMap = getSearchParamMap(user);
        Map<String,Object> returnMap = new HashMap<>();
        List<ChartsSearchView> firstTypeList;
        if(RedisUtil.isOn){
            String key = RedisUtil.getAreaSearchKey(user,"firstType");
            List list = RedisUtil.getListValue(redisTemplate,key);
            if(list == null){
                firstTypeList = chartsAreaSearchDao.queryFirstDeviceTypes(paramMap);
                // 把数据存入缓存
                new RedisListStoreThread(redisTemplate,key,firstTypeList).run();
                for(ChartsSearchView chartsSearchView : firstTypeList){
                    String subKey = key + "_" + chartsSearchView.getId();
                    List<ChartsSearchView> subList = chartsAreaSearchDao.querySecondDeviceTypes(Long.parseLong(chartsSearchView.getId()));
                    // 把数据存入缓存
                    new RedisListStoreThread(redisTemplate,subKey,subList).run();
                    returnMap.put(chartsSearchView.getId(),getPinYin(subList));


                }
                returnMap.put("first",getPinYin(firstTypeList));
            }else{
                firstTypeList = list;
                returnMap.put("first",getPinYin(firstTypeList));
                for(ChartsSearchView chartsSearchView : firstTypeList){
                    String subKey = key + "_" + chartsSearchView.getId();
                    List<ChartsSearchView> subList = RedisUtil.getListValue(redisTemplate,subKey);
                    returnMap.put(chartsSearchView.getId(),getPinYin(subList));
                }
            }
        }else{
            firstTypeList = chartsAreaSearchDao.queryFirstDeviceTypes(paramMap);
            for(ChartsSearchView chartsSearchView : firstTypeList){
                List<ChartsSearchView> subList = chartsAreaSearchDao.querySecondDeviceTypes(Long.parseLong(chartsSearchView.getId()));
                returnMap.put(chartsSearchView.getId(),getPinYin(subList));
            }
            returnMap.put("first",getPinYin(firstTypeList));
        }
        return returnMap;
    }

    @Override
    public List<ChartsSearchView> queryDeviceBrands(User user) {
        Map<String,Object> paramMap = getSearchParamMap(user);
        List<ChartsSearchView> returnList;
        if(RedisUtil.isOn){
            String key = RedisUtil.getAreaSearchKey(user,"brand");
            List list = RedisUtil.getListValue(redisTemplate,key);
            if(list == null){
                returnList = chartsAreaSearchDao.queryDeviceBrands(paramMap);
                returnList.add(new ChartsSearchView("-100","其他","Other"));
                // 把数据存入缓存
                new RedisListStoreThread(redisTemplate,key,returnList).run();
            }else{
                returnList = list;
            }
        }else{
            returnList = chartsAreaSearchDao.queryDeviceBrands(paramMap);
            returnList.add(new ChartsSearchView("-100","其他","Other"));
        }
        return getPinYin(returnList);
    }

    @Override
    public List<ChartsSearchView> queryCurYears() {
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        List<ChartsSearchView> list = new ArrayList<>();
        for(int i=0;i<4;i++){
            ChartsSearchView obj = new ChartsSearchView(String.valueOf(year),String.valueOf(year)+"年",String.valueOf(year));
            year -= 1;
            list.add(obj);
        }
        return list;
    }

    @Override
    public  List<ChartsSearchView> queryPriceCategory(User user) {
        //设置Redis开关
        //RedisUtil.getListValue(redisTemplate,"gable");
        RedisUtil.isOn = false;

        //获取金额区间
        Map<String,Object> paramMap = getSearchParamMap(user);
        paramMap.put("type",1); //金额区间类型为1
        ChartsPriceCategory obj = chartsAreaSearchDao.queryPriceCategory(paramMap);
        if(obj == null){
            paramMap.put("regionCode",310112);
            obj = chartsAreaSearchDao.queryPriceCategory(paramMap);
        }
        //组装筛选条件
        ChartsSearchView csv0 = new ChartsSearchView("0",String.valueOf(obj.getPriceDefault()/1000000),null);
        ChartsSearchView csv1 = new ChartsSearchView("1",String.valueOf(obj.getPriceMax()/1000000),null);
        ChartsSearchView csv2 = new ChartsSearchView("2",String.valueOf(obj.getPriceMin()/1000000),null);
        return Arrays.asList(csv0,csv1,csv2);
    }

}
